from selenium.webdriver.common.by import By
from lib.webui import *
import time
import pytest

GSTORE = {}
def setup_module():
    wd = open_browser()
    login(wd)
    GSTORE['wd'] = wd

def teardown_module():
    wd = GSTORE['wd']
    wd.quit()

class Test_UI0105:

    medicine = ['阿斯匹林','aspl7438','用于治疗某种精神疾病']

    # @classmethod
    # def setup_class(cls):
    #     #创建药品
    #     wd = GSTORE['wd']
    #     wd.find_element(By.XPATH,"//section[@class='sidebar']//ul/li[3]").click()
    #     wd.find_element(By.XPATH,"//section[@class='content container-fluid']/div/button[1]").click()
    #     rs = wd.find_elements(By.XPATH,"//section[@class='content container-fluid']/div//input")
    #     rs[0].send_keys(cls.medicine[0])
    #     rs[1].send_keys(cls.medicine[1])
    #     wd.find_element(By.XPATH,"//section[@class='content container-fluid']/div//textarea").send_keys(cls.medicine[2])
    #     time.sleep(2)
    #     wd.find_elements(By.XPATH,"//section[@class='content container-fluid']//button")[1].click()

    # @classmethod
    # def teardown_class(cls):
    #     #删除添加的药品
    #     wd = GSTORE['wd']
    #     ele = wd.find_elements(By.CSS_SELECTOR,".container-fluid .search-result-item")[0]
    #     ele.find_element(By.CSS_SELECTOR,"label:nth-child(even)").click()
    #     time.sleep(0.5)
    #     wd.switch_to.alert.accept()

    def setup_method(self):
                #创建药品
        wd = GSTORE['wd']
        wd.find_element(By.XPATH,"//section[@class='sidebar']//ul/li[3]").click()
        wd.find_element(By.XPATH,"//section[@class='content container-fluid']/div/button[1]").click()
        rs = wd.find_elements(By.XPATH,"//section[@class='content container-fluid']/div//input")
        rs[0].send_keys(self.medicine[0])
        rs[1].send_keys(self.medicine[1])
        wd.find_element(By.XPATH,"//section[@class='content container-fluid']/div//textarea").send_keys(self.medicine[2])
        time.sleep(2)
        wd.find_elements(By.XPATH,"//section[@class='content container-fluid']//button")[1].click()

    def teardown_method(self):
                #删除添加的药品
        wd = GSTORE['wd']
        ele = wd.find_elements(By.CSS_SELECTOR,".container-fluid .search-result-item")[0]
        ele.find_element(By.CSS_SELECTOR,"label:nth-child(even)").click()
        time.sleep(0.5)
        wd.switch_to.alert.accept()

    def test_UI0105(self):
        wd = GSTORE['wd']
        ele = wd.find_elements(By.CSS_SELECTOR,".container-fluid .search-result-item")[0]
        rs = ele.find_elements(By.CSS_SELECTOR,".search-result-item-field span:nth-child(2)")
        info = [i.text for i in rs ]
        print(info)
        assert info == self.medicine
        
    #这个是自己想来写的，点击编辑按钮，修改之后看是否能保存成功
    @pytest.mark.冒烟
    def test_UI0105_1(self):
        wd = GSTORE['wd']
        ele = wd.find_elements(By.CSS_SELECTOR,".container-fluid .search-result-item")[0]
        ele.find_element(By.CSS_SELECTOR,"label:nth-child(odd)").click()
     
        rs = ele.find_elements(By.CSS_SELECTOR,"input")
        rs[0].clear()
        rs[0].send_keys('啊啊啊死皮林')

        
        accept_btn = ele.find_element(By.CSS_SELECTOR,"label:nth-child(1)")
        wd.execute_script("arguments[0].scrollIntoView({block:'center',inline:'center'})",accept_btn)
        accept_btn.click()

        #收集页面信息
        ele1 = wd.find_elements(By.CSS_SELECTOR,".container-fluid .search-result-item")[0]
        rs1 = ele1.find_elements(By.CSS_SELECTOR,".search-result-item-field span:nth-child(2)")
        info = [i.text for i in rs1 ]
        expect = ['啊啊啊死皮林','aspl7438','用于治疗某种精神疾病']
        assert expect == info
        
